package com.xiaohudie.Ioliu;

import org.junit.jupiter.api.Test;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author xiao hu die
 * @Date 2022/1/6 20:36
 * @Version 1.0
 */
public class Test01 {
    public static void main(String[] args) {

    }
    @Test
    public void test01() throws Exception {
    String filePath ="D:\\sorry.txt";
    int date =0;
        FileReader fileReader =null;
        fileReader =new FileReader(filePath);
        while ((date= fileReader.read())!=-1) {
         System.out.print((char) date);
        }if (fileReader!=null){
            fileReader.close();
        }
    }
    @Test
    public void test02() throws Exception {
        String filePath ="D:\\sorry.txt";
        int date =0;
        char[] chars=new char[8];
        FileReader fileReader =null;
        fileReader =new FileReader(filePath);
        while ((date= fileReader.read(chars))!=-1) {
            System.out.print(new String(chars,0,date));
        }if (fileReader!=null){
            fileReader.close();
        }
    }
    @Test
    public void test03(){
        String filePath ="D:\\note.txt";
        FileWriter fileWriter =null;
        char[] chars ={'a','b','c'};
        try {
            fileWriter = new FileWriter(filePath);
            fileWriter.write('H');
            fileWriter.write(chars);
            fileWriter.write("李家龙喜欢刘碟".toCharArray(),1,5);
            fileWriter.write("你好刘碟");
            fileWriter.write("刘碟刘碟刘碟",1,5);
            System.out.println("写入成功");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (fileWriter!=null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
